package com.example.demo;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

/**
 * @author: hanxuan
 * @date: 2020/12/2
 */

public class AddUserActivity extends AppCompatActivity implements View.OnClickListener {
    private MyDatabaseHelper dbHelper;
    private EditText username,telnumber;
    private Button ok_add;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_user);

        dbHelper = new MyDatabaseHelper(this,"Use.db",null,1);
        username = findViewById(R.id.add_user_name);
        telnumber = findViewById(R.id.add_user_telnumber);

        ok_add = findViewById(R.id.ok_add);
        ok_add.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ok_add:{
                String name = username.getText().toString();
                String tel_number = telnumber.getText().toString();
                if (name == null||tel_number == null){
                    Toast.makeText(this,"用户名和电话不能为空",Toast.LENGTH_SHORT).show();
                }else {
                    SQLiteDatabase db = dbHelper.getReadableDatabase();
                    ContentValues values = new ContentValues();
                    values.put("name",name);
                    values.put("telnumber",tel_number);
                    db.insert("Use",null,values);
                    Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show();

                    this.finish();
                    db.close();
                }
            }

        }
    }
}
